package cc.facesign.oss.service.impl;

import cc.facesign.oss.service.OssService;
import cc.facesign.oss.util.PropertiesUtil;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import org.joda.time.DateTime;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

@Service
@Transactional
public class OssServiceImpl implements OssService {
    @Override
    public String upload(MultipartFile file) {
        String endpoint = PropertiesUtil.END_POINT;
        String accessKeyId = PropertiesUtil.KEY_ID;
        String accessKeySecret = PropertiesUtil.KEY_SECRET;
        String bucketName = PropertiesUtil.BUCKET_NAME;

        String objectName = UUID.randomUUID().toString().replaceAll("-", "") + file.getOriginalFilename();
        String date = new DateTime().toString("yyyy/MM/dd");
        objectName = date + "/" + objectName;
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);


        InputStream inputStream = null;
        try {
            inputStream = file.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 创建PutObject请求。
        ossClient.putObject(bucketName, objectName, inputStream);

        String url = "https://" + bucketName + "." + endpoint + "/" + objectName;
        return url;
    }

    @Override
    public String uploadByUrl(String url) {
       // System.out.println("************" + url);
        File file = new File(url);

        String endpoint = PropertiesUtil.END_POINT;
        String accessKeyId = PropertiesUtil.KEY_ID;
        String accessKeySecret = PropertiesUtil.KEY_SECRET;
        String bucketName = PropertiesUtil.BUCKET_NAME;

        String objectName = UUID.randomUUID().toString().replaceAll("-", "") + file.getName();
        String date = new DateTime().toString("yyyy/MM/dd");
        objectName = date + "/" + objectName;

      //  System.out.println(objectName);
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);


        InputStream inputStream = null;
        try {
            inputStream = new FileInputStream(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 创建PutObject请求。
        ossClient.putObject(bucketName, objectName, inputStream);


        String res = "https://" + bucketName + "." + endpoint + "/" + objectName;
       // System.out.println(res);
        return res;
    }

    @Override
    public String uploadByFileStream(InputStream inputStream, String suffix) {
        String endpoint = PropertiesUtil.END_POINT;
        String accessKeyId = PropertiesUtil.KEY_ID;
        String accessKeySecret = PropertiesUtil.KEY_SECRET;
        String bucketName = PropertiesUtil.BUCKET_NAME;

        String objectName = UUID.randomUUID().toString().replaceAll("-", "") + "." + suffix;
        String date = new DateTime().toString("yyyy/MM/dd");
        objectName = date + "/" + objectName;
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        // 创建PutObject请求。
        ossClient.putObject(bucketName, objectName, inputStream);

        String url = "https://" + bucketName + "." + endpoint + "/" + objectName;
        return url;
    }
}
